package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.lemeng.dao.PreferentialMapper;
import com.lemeng.pojo.Category;
import com.lemeng.pojo.OrderItem;
import com.lemeng.pojo.Preferential;
import com.lemeng.pojo.Sku;
import com.lemeng.service.CartService;
import com.lemeng.service.CategoryService;
import com.lemeng.service.PreferentialService;
import com.lemeng.service.SkuService;
import com.lemeng.util.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class PreferentialServiceImpl implements PreferentialService {

    @Autowired
    PreferentialMapper preferentialMapper;
    @Override
    public Integer findCategoryPreMoney(Integer catrgoryId, Integer money) {
        Example example = new Example(Preferential.class);
        example.setOrderByClause("buy_money desc");
        Date date = new Date();
        Example.Criteria criteria = example.createCriteria();
        criteria.andLessThanOrEqualTo("startTime",date)
                .andGreaterThanOrEqualTo("endTime",date)
                .andEqualTo("state",1)
                .andEqualTo("categoryId",catrgoryId)
                .andLessThanOrEqualTo("buyMoney",money);

        List<Preferential> preferentials = preferentialMapper.selectByExample(example);

        int preMoney = 0;

        if (!CollectionUtils.isEmpty(preferentials)){
            Preferential preferential = preferentials.get(0);

            preMoney = preferential.getPreMoney();//不翻倍情况

            if (preferential.getType().equals("2")){
             preMoney = money/preferential.getBuyMoney()*preferential.getPreMoney();
            }
        }


        return preMoney;
    }
}
